<?php
class CommenstForApprovalContainerForm extends sfForm
{
    public function __construct($comments = array(), $options = array(), $CSRFSecret = null)
    {
        $this->setDefaults(array());
        $this->options = $options;
        $this->localCSRFSecret = $CSRFSecret;
        $this->comments = $comments;

        $this->validatorSchema = new sfValidatorSchema();
        $this->widgetSchema    = new sfWidgetFormSchema();
        $this->errorSchema     = new sfValidatorErrorSchema($this->validatorSchema);

        $this->setup();
        
        $this->configure($comments);

        $this->addCSRFProtection($this->localCSRFSecret);
        $this->resetFormFields();
    }

    public function configure()
    {
        $wrapperForm = new sfForm();

        foreach($this->comments as $key=>$comment){
            $wrapperForm->embedForm($key, new CommentApprovalForm($comment));
        }

        $this->embedForm('comments', $wrapperForm);

        $this->widgetSchema->setNameFormat('comments_list[%s]');
    }

    public function save()
    {
        $values = $this->getValues();

        foreach($this->embeddedForms['comments']->getEmbeddedForms() as $key=>$commentForm)
        {
            if ($values['comments'][$key])
            {
                $commentForm->updateObject($values['comments'][$key]);
                $commentForm->getObject()->save();
            } 
        }
    }

}

